package com.lottery.game.controller.v2.find;


import com.lottery.game.common.Response;
import com.lottery.game.common.exception.BaseException;
import com.lottery.game.controller.v2.find.request.FindPasswordTO;
import com.lottery.game.core.model.request.register.VerifyCodeRequest;
import com.lottery.game.core.modules.entity.user.GameUser;
import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.app.v2.MessageSendAndCheckService;
import com.lottery.game.core.validator.annotation.Validate;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

import io.swagger.annotations.ApiOperation;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2019-02-13 20:03
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.runshang.apps.cloudgame.controller.v2.find.FindPasswordController
 */
@RestController
@RequestMapping("/api/v2/find")
@Api(tags = {"APP-用户找回密码"})
public class FindPasswordController {

    @Autowired
    private MessageSendAndCheckService messageSendAndCheckService;

    @Autowired
    private GameUserService gameUserService;

    @ApiOperation(value = "发送找回密码手机验证码-返回TOKEN需要在验证手机号时传递")
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Response<String> send(@RequestParam("phone") String phone) {
        //找回密码之前，判断是否注册过
        GameUser gameUser = gameUserService.getGameUserByUserName(phone);
        if (gameUser == null) {
            throw new BaseException("该手机号尚未注册");
        }
        return Response.build(messageSendAndCheckService.send(phone, "找回密码"));
//        return Response.buildSuccess();
    }

    @ApiOperation(value = "重置密码", notes = "body参数:\n{\n" + "    \"code\": \"5209\",\n" + "    \"password\": \"123456\",\n" + "    \"phone\": \"15348304228\",\n" + "    \"rePassword\": \"123456\",\n" + "    \"token\": \"eyJzaWduIjoiNGU2MDE1ZGVmMjY2N2NkOWQyMzgxZGIzZjcyMjRiNDIiLCJ0aW1lc3RhbXAiOiIxNTU1Mjk5MDYyNTE5In0\"\n" + "}")
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Validate
    public Response<Boolean> find(@RequestBody FindPasswordTO findPasswordTO) {
        findPasswordTO.validate();
        GameUser gameUser = gameUserService.getGameUserByUserName(findPasswordTO.getPhone());
        messageSendAndCheckService.check(findPasswordTO.getToken(), findPasswordTO.getCode(), findPasswordTO.getPhone());
        Map<String, Object> result = gameUserService.updateNewPwd(findPasswordTO.getPhone(), gameUser.getUsername(), findPasswordTO.getPassword());
        return Response.build(Integer.valueOf(result.get("code").toString()), result.get("message").toString(), Objects.equals(result.get("status"), "success"), false);
    }
    
    /**
	 * 验证验证码是否通过
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "验证验证码是否通过", notes = "验证验证码是否通过" +
			"\n    body参数:\n{\n" + "    \"code\": \"5209\",\n" + "    \"phone\": \"15348304229\",\n" + "    \"token\": \"eyJzaWduIjoiNGU2MDE1ZGVmMjY2N2NkOWQyMzgxZGIzZjcyMjRiNDIiLCJ0aW1lc3RhbXAiOiIxNTU1Mjk5MDYyNTE5In0\"\n" + "}", httpMethod="POST")
	@PostMapping(value = "/validate/code", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	@Validate
	public Response<Boolean> validateCode(@RequestBody VerifyCodeRequest request) {
		return Response.build(this.validateCode(request.getPhone(), request.getCode(), request.getToken()));
	}
	

	/**
	 * 校验手机验证码
	 */
	private boolean validateCode(String phone, String code, String token) {
		 return messageSendAndCheckService.preCheck(token, code, phone) != null;
	}
}
